package com.zhiyou100.dao;

import com.zhiyou100.tool.Tools;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @packageName: TeacherDao
 * @className: com.zhiyou100.dao
 * @Description: TODO TeacherDao
 * @author: yang
 * @date: 7/20/20
 */
public class TeacherDao {
    /**
     * @Description: TODO 判断是否登陆成功
     * @name: loginABoolean
     * @param: [tname, tpwd]
     * @return: boolean
     * @date: 7/20/20 17:47
     * @author: yang
     */
    public boolean flagOfLogin(String tname, String tpwd) {
        // 记录是否登陆成功
        boolean flag = false;
        // con,sta,res
        Connection connection = Tools.getConnection();
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            // 发送模版
            statement = connection.prepareStatement("select tid from Teacher where tname=? and tpwd=?");
            statement.setString(1, tname);
            statement.setString(2, tpwd);
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                flag = true;
            } else {
                flag = false;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            Tools.close(resultSet, statement, connection);
        }
        return flag;
    }
    /**
     * @Description: TODO  判断是否存在 tid
     * @name: theTidExist
     * @param: [tid]
     * @return: boolean
     * @date: 7/23/20 11:52
     * @author: yang
     */
    public boolean theTidExist(String tid) {
        // 记录是否登陆成功
        boolean flag=false;
        Connection connection = Tools.getConnection();
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            // 发送模版
            statement = connection.prepareStatement("select * from Teacher where tid=?;");
            statement.setInt(1, Integer.parseInt(tid));
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                flag = true;
            } else {
                flag = false;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            Tools.close(resultSet, statement, connection);
        }
        return flag;
    }

    /**
     * @Description: TODO 根据 名字 获取 tid，若没有 tid ，则返回 -1
     * @name: getTheIDByName
     * @param: [sname]
     * @return: int
     * @date: 7/24/20 17:34
     * @author: yang
     */
    public int getTheIDByName(String sname) {
        Connection connection = Tools.getConnection();
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        // 记录 tid
        int tid=0;
        try {
            // 发送模版
            statement = connection.prepareStatement("select tid from Teacher where tname=?;");
            statement.setString(1,sname);
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                tid=resultSet.getInt(1);
            } else {
                tid=-1;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            Tools.close(resultSet, statement, connection);
        }
        return tid;
    }




    public static void main(String[] args) {
        TeacherDao teacherDao = new TeacherDao();
        int tid = teacherDao.getTheIDByName("杨2");
        System.out.println(tid);
    }
}
